package com.zzq.demo.sign;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author : zhaoziqian
 * @description :
 * @date : 2019/9/24 17:23
 */
public class SignUtil {

    public static String createAppId() {
        String yyyyMMdd = new SimpleDateFormat("yyyyMMdd").format(new Date());
        return yyyyMMdd + SnowFlake.getInstance(1L,1L).nextId();
    }

    public static String createAppSecret() {
        String s1 = UUID.randomUUID().toString().replaceAll("-", "");
        String s2 = UUID.randomUUID().toString().replaceAll("-", "");
        return s1.substring(0,20) + s2.substring(0,20);
    }

    public static String sign(String appId,String appKey, String ts,String version,String json) {
        String data = JSON.toJSONString(JSONObject.parse(json), SerializerFeature.MapSortField);
        String str = appKey + "appId" + appId + "ts" + ts + "version" + version + data;
        return SHA1.encode(str);
    }
}
